﻿using GetValue.poe_ninja_api;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;

namespace tools.Dev
{
    public class Api
    {
        public static NinjaData Ninja;
        public static bool DownloadDone = false;

        public static string[] GetLeagues()
        {
            string[] ret;
            using (WebClient web = new WebClient())
            {
                string text = web.DownloadString("http://dev.inic.top/api/Data/Get?Overview=leagues");
                ret = text.Split('|');
            }
            return ret;
        }

        public static void DownloadData(string league)
        {
            DownloadDone = false;
            Ninja = new NinjaData();

            using (WebClient web = new WebClient())
            {

                using (Stream webStream = web.OpenRead($"http://dev.inic.top/api/Data/Get?Overview=all&league={league}"))
                {
                    using (ZipArchive archive = new ZipArchive(webStream, ZipArchiveMode.Read))
                    {
                        foreach (ZipArchiveEntry entry in archive.Entries)
                        {
                            using (Stream stream = entry.Open())
                            {
                                using (StreamReader reader = new StreamReader(stream))
                                {
                                    string jsonString = reader.ReadToEnd();
                                    switch (entry.FullName)
                                    {
                                        case "Currency":
                                            Ninja.Currency = JsonToClass<Currency.RootObject>(jsonString);
                                            break;
                                        case "DivinationCards":
                                            Ninja.DivinationCards = JsonToClass<DivinationCards.RootObject>(jsonString);
                                            break;
                                        case "Essence":
                                            Ninja.Essences = JsonToClass<Essences.RootObject>(jsonString);
                                            break;
                                        case "Fragment":
                                            Ninja.Fragments = JsonToClass<Fragments.RootObject>(jsonString);
                                            break;
                                        case "Prophecy":
                                            Ninja.Prophecies = JsonToClass<Prophecies.RootObject>(jsonString);
                                            break;
                                        case "UniqueAccessory":
                                            Ninja.UniqueAccessories = JsonToClass<UniqueAccessories.RootObject>(jsonString);
                                            break;
                                        case "UniqueArmour":
                                            Ninja.UniqueArmours = JsonToClass<UniqueArmours.RootObject>(jsonString);
                                            break;
                                        case "UniqueFlask":
                                            Ninja.UniqueFlasks = JsonToClass<UniqueFlasks.RootObject>(jsonString);
                                            break;
                                        case "UniqueJewel":
                                            Ninja.UniqueJewels = JsonToClass<UniqueJewels.RootObject>(jsonString);
                                            break;
                                        case "UniqueMap":
                                            Ninja.UniqueMaps = JsonToClass<UniqueMaps.RootObject>(jsonString);
                                            break;
                                        case "UniqueWeapon":
                                            Ninja.UniqueWeapons = JsonToClass<UniqueWeapons.RootObject>(jsonString);
                                            break;
                                        case "Map":
                                            Ninja.WhiteMaps = JsonToClass<WhiteMaps.RootObject>(jsonString);
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            DownloadDone = true;
        }

        private static T JsonToClass<T>(string jsonString)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            T jsonObject = (T)ser.ReadObject(ms);
            ms.Close(); 
            return jsonObject;
        }
    }

    [DataContract]
    public class Leagues
    {
        [DataMember]
        public string id { get; set; }
    }

    public class NinjaData
    {
        public Currency.RootObject Currency { get; set; }
        public DivinationCards.RootObject DivinationCards { get; set; }
        public Essences.RootObject Essences { get; set; }
        public Fragments.RootObject Fragments { get; set; }
        public Prophecies.RootObject Prophecies { get; set; }
        public UniqueAccessories.RootObject UniqueAccessories { get; set; }
        public UniqueArmours.RootObject UniqueArmours { get; set; }
        public UniqueFlasks.RootObject UniqueFlasks { get; set; }
        public UniqueJewels.RootObject UniqueJewels { get; set; }
        public UniqueMaps.RootObject UniqueMaps { get; set; }
        public UniqueWeapons.RootObject UniqueWeapons { get; set; }
        public WhiteMaps.RootObject WhiteMaps { get; set; }
    }


}
